﻿@inject IArticleService _articleService
@inject IVideoService _videoService
@implements IDisposable
@inject IDataCacheService _dataCacheService
@inject IHttpService _httpService

<div>
    <div>@($"{item.Url} ({index}/{Model.Count})")</div>
    <MProgressLinear Value="index*100.0/Model.Count" Class="mb-2" Stream BufferValue="0" ></MProgressLinear>
</div>
<div>
    <div>@($"{infor} ({item.CompleteTaskCount}/{item.TotalTaskCount})")</div>
    <MProgressLinear Value="item.CompleteTaskCount*100.0/item.TotalTaskCount" Stream BufferValue="0" ></MProgressLinear>
</div>

@code {
    [Parameter]
    public List<RepostVideoModel> Model { get; set; }
    [Parameter]
    public string Class { get; set; }

    public IEnumerable<string> VideoItems { get; set; }
    public IEnumerable<string> EntryGameItems { get; set; }

    int index = 0;
    string infor = "";
    RepostVideoModel item = new RepostVideoModel();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public async void OnProgressUpdate(KeyValuePair<OutputLevel, string> model)
    {

        try
        {
            await InvokeAsync(() =>
            {
                infor = model.Value;
                StateHasChanged();
            });
        }
        catch
        {

        }
    }

    public async Task Proc()
    {
        try
        {
            //获取所有词条
            EntryGameItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/0");
            VideoItems = await _httpService.GetAsync<List<string>>("api/videos/GetNames");

            _videoService.ProgressUpdate += OnProgressUpdate;

            for (index = 0; index < Model.Count; index++)
            {
                item = Model[index];
                await _videoService.ProcVideo(item, VideoItems, EntryGameItems);
            }

        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取所有词条视频名称失败");
            return;
        }
        finally
        {
            _articleService.ProgressUpdate -= OnProgressUpdate;
        }
    }

    #region 释放实例
    public void Dispose()
    {
        _articleService.ProgressUpdate -= OnProgressUpdate;
        GC.SuppressFinalize(this);
    }
    #endregion

}
